/**
* Copyright @ 2023 iAuto (Shanghai) Co., Ltd.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are NOT permitted except as agreed by
* iAuto (Shanghai) Co., Ltd.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/


import {Col,Button, message, Popconfirm, Row, Space, TableColumnsType, Tooltip} from 'antd';

import React from 'react';
import IAutoTable from '@/components/soa/IAutoTable';
import { useAntdTable, useRequest } from 'ahooks';
import { router } from 'umi';
import IAutoPage from '@/components/soa/IAutoPage';
import style from './releaseHistory.less';
import {downService, getScenesList1} from '@/services/soa/sceneOfficial';
import {IReleaseHistory} from "@/types/sceneOfficial";
import {getDateMinute, getDateTime} from "@/utils/soa/utils";

const Index = ({location}:any) => {
  const { query } = location;
  const { refresh, tableProps } = useAntdTable(getScenesList1,{
    defaultParams: [{current:1,pageSize:10,sceneUuid:query?.id}],
  });
  const detailHandle = (record: any) => {
    router.push({
      pathname: '/soaManage/scene/official/releaseDetail',
      query: {
        id: record.sceneRecordUuid,
        name: record.name,
      },
    });
  };

  enum statusTypes {
    '上架审核中',
    '下架审核中',
    '上架成功',
    '上架失败',
    '下架成功',
    '下架失败',
    '定时上架中',
  }

  enum releaseTypes {
    '定时发布',
    '立即发布',
  }

  const downRequest = useRequest(downService, {
    manual: true,
    onSuccess(response) {
      if (response?.statusCode=='0') {
        message.success('申请成功，已提交审核');
      }
      refresh();
    },
  });

  const columns: TableColumnsType<IReleaseHistory> = [
    {
      title: '场景名',
      dataIndex: 'name',
      ellipsis: true,
    },
    {
      title: '描述',
      dataIndex: 'description',
      ellipsis: true,
    },
    {
      title: '品牌',
      dataIndex: 'brandName',
    },
    {
      title: '车系',
      dataIndex: 'seriesName',
      ellipsis: true,
      render: (_, item) => {
        return <span>{item.seriesName}</span>;
      },
    },
    {
      title: '车型',
      dataIndex: 'modelName',
      ellipsis: true,
      width:150,
      render: (_, {modelName}) => {
        return (
          <Tooltip title={modelName}>
            <span className={style.reasonSpan}>{modelName}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '申请发布时间',
      dataIndex: 'applyTime',
      ellipsis: true,
      render: (text) => getDateTime(text),
    },
    {
      title: '发布类型',
      dataIndex: 'releaseType',
      ellipsis: true,
      render: (_, item) => {
        return <span>{releaseTypes[item.releaseType]}</span>;
      },
    },
    {
      title: '期望发布时间',
      dataIndex: 'cronReleaseTime',
      ellipsis: true,
      render: (_, { cronReleaseTime: time}) => {
        if (time === null) {
          return <span>-</span>
        } else {
          return getDateMinute(time)
        }
      },
    },
    {
      title: '状态',
      dataIndex: 'auditStatus',
      render: (_, { auditStatus: status,rejectReason}) => {
        // 0-上架审核中(上架申请)，1-下架审核中（下架申请），2-上架成功，3-上架失败，4-下架成功，5-下架失败
        if (status == 2 || status == 4 || status == 6) {
          return <span style={{color:'#00a870'}}>{statusTypes[status]}</span>
        }
        else if (status == 0 || status == 1) {
          return <span style={{color:'#3569FD'}}>{statusTypes[status]}</span>
        }else if(status == 3|| status == 5){
          return (
            <div>
              <div>
                <span style={{color:'#FF7918'}}>{statusTypes[status]}</span>
              </div>
              <div className={style.reasonBox}>
                <Tooltip title={rejectReason}>
                  <span className={style.reasonSpan}>原因：{rejectReason}</span>
                </Tooltip>
              </div>
            </div>
          );

        }
      },
    },
    {
      title: '操作',
      key: 'operation',
      width: 200,
      render: (_, record) => {
        if (record.auditStatus === 2 || record.auditStatus === 5) {
          return (
            <Space>
              <Button type={'primary'} ghost size={'small'} onClick={() => detailHandle(record)}>查看</Button>
              <Popconfirm
                title={`请确定是否下架?`}
                onConfirm={() => {
                  const data = {
                    sceneRecordUuid: record.sceneRecordUuid,
                  };
                  downRequest.run({ ...data });
                }}
              >
                <Button type={'primary'} ghost size={'small'}>
                  下架
                </Button>
              </Popconfirm>

            </Space>
          );
        } else {
          return (
            <Space>
              <Button type={'primary'} ghost size={'small'} onClick={() => detailHandle(record)}>查看</Button>
            </Space>
          );
        }
      },
    },
  ];

  return (
    <IAutoPage className={style.main}>
      <Row>
        <Col span={24}>
          <IAutoTable columns={columns} tableProps={tableProps} size="small"></IAutoTable>
        </Col>
      </Row>
    </IAutoPage>
  );
};

export default Index;
